package com.sgb.kjwl.view.ui.adapter.manage

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import com.sgb.kjwl.BR
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ItemSelfAddBinding
import com.sgb.kjwl.model.entity.manage.AddItemEntity
import com.sgb.kjwl.view.widget.lease.common.CommonBaseAdapter
import com.sgb.kjwl.view.widget.lease.common.CommonItemViewHolder
import com.swgk.core.customview.CfkEditView

class AddViewAdapter(context : Context) : CommonBaseAdapter<AddItemEntity, CommonItemViewHolder>(context) {

    private var mCallBack : OnDeleteCallBack ?= null
    private var isEdit = true

    fun setCallBack(callBack : OnDeleteCallBack){
        this.mCallBack = callBack
    }

    fun setEnableEdit(isEdit : Boolean){
        this.isEdit = isEdit
    }

    override fun onCreateViewHolderImpl(parent: ViewGroup?, viewType: Int): CommonItemViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_self_add, parent, false)
        val viewHolder = CommonItemViewHolder(view)
        view.findViewById<ImageView>(R.id.iv_delete).setOnClickListener {
            if (mCallBack != null){
                mCallBack!!.delete(viewHolder.layoutPosition)
            }
        }
        view.findViewById<EditText>(R.id.et_title).setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus){
                if (mCallBack != null){
                    mCallBack!!.onFocus(viewHolder.layoutPosition)
                }
            }

        }
        view.findViewById<EditText>(R.id.et_title).isEnabled = isEdit
        view.findViewById<CfkEditView>(R.id.item_tv_content).setEnable(isEdit)
        view.findViewById<ImageView>(R.id.iv_delete).visibility = if (isEdit) View.VISIBLE else View.GONE
        return viewHolder
    }


    override fun onBindViewHolderImpl(holder: CommonItemViewHolder?, position: Int) {
          val item = getItem(position)
          if (!isEdit && TextUtils.isEmpty(item.fieldValue)){
              item.fieldValue = "-"
          }
          holder!!.binding.setVariable(BR.viewModel, item)
          holder.itemView.findViewById<View>(R.id.view_line).visibility = if (arrayData.size - 1 == position) View.GONE else View.VISIBLE
    }

    interface OnDeleteCallBack{
        fun delete(position: Int)
        fun onFocus(position: Int)
    }
}